//
//  SISSAboutUsViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/4.
//  CopysubTitle © 2017年 优房网. All subTitles reserved.
//

#import "SISSAboutUsViewController.h"
#import "SISSAboutUsWebViewController.h"
#import "SISSAboutUsModel.h"

@interface SISSAboutUsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *appNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *versionsLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/* 数据源数组 */
@property (copy, nonatomic) NSArray<SISSAboutUsModel *> *dataArray;


/* textViewArr */
@property (strong, nonatomic) NSMutableArray<UITextView *> *textViewArr;

@end

@implementation SISSAboutUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.scrollEnabled = NO;
    self.tableView.bounces = NO;
    
    self.versionsLabel.text = [NSString stringWithFormat:@"版本号：%@",kBundleVersionStr];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseID = @"reuseID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    UITextView *textView;
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
        
        textView = [[UITextView alloc] init];
        textView.scrollEnabled = NO;
        textView.editable = NO;
        textView.font = [UIFont systemFontOfSize:17];
        textView.textAlignment = NSTextAlignmentRight;
        textView.textColor = MAIN_TEXT_COLOR;
        textView.tag = indexPath.row;
        [cell.contentView addSubview:textView];
        
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell.contentView addSubview:lineView];
        
        [textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView).offset(-8);
            make.centerY.equalTo(cell.contentView);
//            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(cell.contentView);
            make.height.mas_equalTo(1);
        }];
        
        [self.textViewArr addObject:textView];
    }

    cell.textLabel.text = self.dataArray[indexPath.row].title;
    textView.text = self.dataArray[indexPath.row].subTitle;
    
    if (indexPath.row > 1) {
        textView.textColor = MAIN_COLOR;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        [textView addGestureRecognizer:tap];
    }
    
    
    return cell;
}

#pragma mark -- 点击跳转网页
- (void)tapGestureAction:(UITapGestureRecognizer *)sender {
    
//    NSLog(@"%@",sender.view);
    UITextView *textView = (UITextView *)sender.view;
    
    SISSAboutUsWebViewController *vc = [[SISSAboutUsWebViewController alloc] init];
    vc.title = self.dataArray[textView.tag].title;
    vc.url = textView.text;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    for (UITextView *textView in self.textViewArr) {
        textView.selectedRange = NSMakeRange(0, 0);
    }
//    if (indexPath.row < 2) return;
//    
//    SISSAboutUsWebViewController *vc = [[SISSAboutUsWebViewController alloc] init];
//    
//    vc.title = self.dataArray[indexPath.row].title;
//    vc.url = self.dataArray[indexPath.row].subTitle;
//    
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITextView *textView in self.textViewArr) {
        textView.selectedRange = NSMakeRange(0, 0);
    }
}

#pragma mark - 懒加载
- (NSArray<SISSAboutUsModel *> *)dataArray {
    if (!_dataArray) {
        
        NSArray *arr = @[
                         @{@"title": @"微信订阅号", @"subTitle":@"srba83545354"},
                         @{@"title": @"会员服务号", @"subTitle":@"SRBAVIP"},
                         @{@"title": @"协会网站", @"subTitle":@"www.szfzx.org"},
                         @{@"title": @"深圳房地产网", @"subTitle":@"www.szfdc.net.cn"},
                         @{@"title": @"综合服务系统", @"subTitle":@"oa.szfzx.org"}
                         ];
        _dataArray = [NSArray yy_modelArrayWithClass:[SISSAboutUsModel class] json:arr];
    }
    return _dataArray;
}

- (NSMutableArray<UITextView *> *)textViewArr {
    if (!_textViewArr) {
        _textViewArr = [NSMutableArray arrayWithCapacity:5];
    }
    return _textViewArr;
}

@end
